﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Sys_projectapprove : System.Web.UI.Page
{
    PM.PMBusiness.projectapprove project = new PM.PMBusiness.projectapprove();//实例化project
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["tid"] == null || Session["name"] == null || Session["sex"] == null || Session["depart"] == null || Session["usertype"] == null)
        {
            PM.Code.Web.alertmessage.ShowMsg("账号登录已超时，请返回重新登录。", "../Index.aspx");
            return;
        }
        if (!IsPostBack)
        {
            selectbind();
            gridbind();
        }
    }
    //分页绑定
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
            gridbind();
    }
    //数据绑定
    protected void gridbind()
    {
        if (ddlPcode.SelectedValue!="")
        {
            if (Session["usertype"].ToString() == "2")
            {
                Panel1.Visible = true;
                //获取学院允许通过的最大项目数
                int totalnumber = project.getnumber(ddlDepart.SelectedValue.ToString(), ddlPcode.SelectedValue.ToString());
                //获取目前已经通过的项目数
                int passednumber = project.getpassednumber(ddlDepart.SelectedItem.Text.ToString(), ddlPcode.SelectedValue.ToString());
                if (passednumber < totalnumber)
                {
                    lblTotal.Text = totalnumber.ToString();
                    lblNumber.Text = (totalnumber - passednumber).ToString();
                }
                else
                {
                    lblTotal.Text = totalnumber.ToString();
                    lblNumber.Text = "0";
                }
            }
            StringBuilder sqlstrfield = new StringBuilder();
            StringBuilder sqlstrwhere = new StringBuilder();
            getField(ref sqlstrfield);
            getWhere(ref sqlstrwhere);
            PM.Code.Web.controlbind.BindGridViewPublicProc(GridView1, AspNetPager1, "View_Projects", "id", sqlstrfield.ToString(), "id", sqlstrwhere.ToString(), true);
        }     
    }
    //设置查询字段
    protected void getField(ref StringBuilder sqlstrfield)
    {
        sqlstrfield.Append("id,pname,type,plevel,pcollege,name,title,attached,case status when 1 then '待初审' when 21 then '终审退回' when 20 then '初审退回' when 2 then '待终审' end as thestatus");
    }
    //设置查询条件
    protected void getWhere(ref StringBuilder sqlstrwhere)
    {
        sqlstrwhere.Append("1=1");
        //判断用户权限，显示相应数据
        if (Session["usertype"].ToString() == "2")
        {
            sqlstrwhere.Append(" and (status=1 or status=21)");
        }
        else if (Session["usertype"].ToString() == "3")
        {
            sqlstrwhere.Append(" and status=2");
        }
        if (ddlDepart.SelectedValue!="")
        {
            sqlstrwhere.AppendFormat(" and pcollege='{0}'",ddlDepart.SelectedItem.Text.ToString());
        }
        if (ddlLevel.SelectedValue!="")
        {
            sqlstrwhere.AppendFormat(" and plevel='{0}'", ddlLevel.SelectedValue.ToString());
        }
        if (ddlType.SelectedValue!="")
        {
            sqlstrwhere.AppendFormat(" and type='{0}'",ddlType.SelectedValue.ToString());
        }
        if (txtName.Text.ToString().Trim()!="")
        {
            sqlstrwhere.AppendFormat(" and name='{0}'",txtName.Text.ToString().Trim());
        }
        if (ddlPcode.SelectedValue != "")
        {
            sqlstrwhere.AppendFormat(" and pcode='{0}'", ddlPcode.SelectedValue.ToString());
        }
        if (txtTopic.Text.ToString().Trim()!="")
        {
            sqlstrwhere.AppendFormat(" and pname like '%{0}%'",txtTopic.Text.ToString().Trim());
        }
    }
    //下拉框数据绑定
    protected void selectbind()
    {
        string str1 = "select collegeid,collegename from Dict_College";
        string str2 = "select btnname from Sys_Guide";
        StringBuilder str3 = new StringBuilder();
        str3.Append("select plancode from Bus_ApprovePlan where opentime<'").Append(DateTime.Now.ToString());
        str3.Append("' and closetime>'").Append(DateTime.Now.ToString()).Append("' and status=1 order by builddate desc"); 
        PM.Code.Web.controlbind.BindDropDownList(ddlDepart, str1, "collegename", "collegeid");
        PM.Code.Web.controlbind.BindDropDownList(ddlType, str2, "btnname", "btnname");
        PM.Code.Web.controlbind.BindDropDownList(ddlPcode, str3.ToString(), "plancode", "plancode");
        //如果为学院管理员权限，锁定学院下拉框
        if (Session["usertype"].ToString() == "2" && Session["depart"]!=null)
            {
                ddlDepart.SelectedIndex = ddlDepart.Items.IndexOf(ddlDepart.Items.FindByText(Session["depart"].ToString().Trim()));
                ddlDepart.Enabled = false;
            }
    }

    protected void btnSeach_Click(object sender, ImageClickEventArgs e)
    {
            gridbind();
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string pid = e.CommandArgument.ToString().Trim();
        int newstatus = 1;
        int oldstatus=1;//默认待初审状态
        int theresult=0;
        if (e.CommandName=="Allow")
        {
            if ((Session["usertype"].ToString() != "1"&&Session["usertype"].ToString() != "2")||int.Parse(lblNumber.Text.ToString())>0)
            {
                 if (Session["usertype"].ToString() == "2")
                {
                    oldstatus=1;//原状态为待初审状态
                    newstatus = 2;//新状态为待终审状态
                }
                else if (Session["usertype"].ToString() == "3")
                {
                    oldstatus=2;//原状态为待终审状态
                    newstatus = 3;//新状态为待中期检查状态
                }
                else
                {
                     PM.Code.Web.alertmessage.ShowMsg(UpdatePanel1, "你没有相应的操作权限，该操作要求必须权限为学院管理员或项目管理员。");
                     return;
                }
               theresult=project.projectoperate(oldstatus,newstatus,int.Parse(pid));
                if (theresult==1)
                {
                    gridbind();
                }
                else if (theresult==99)
                {
                    PM.Code.Web.alertmessage.ShowMsg(UpdatePanel1, "数据库操作出错，请联系管理员。");
                }
                else if (theresult==0)
                {
                    PM.Code.Web.alertmessage.ShowMsg(UpdatePanel1, "操作失败，可能是权限不够或此项目的状态已被他人修改，请刷新页面后再试。");
                }
            } 
            else
            {
                PM.Code.Web.alertmessage.ShowMsg(UpdatePanel1, "已经到达了本学院所允许通过的最大项目数，请检查已通过的项目列表。");
            }

        } 
        else if(e.CommandName=="Deny")
        {
            if (Session["usertype"].ToString() == "2")
            {
                oldstatus = 1;//原状态为待初审状态
                newstatus = 20;//新状态为退回待填状态
            }
            else if (Session["usertype"].ToString() == "3")
            {
                oldstatus = 2;//原状态为待终审状态
                newstatus = 21;//新状态为退回待初审状态
            }
            else
            {
                PM.Code.Web.alertmessage.ShowMsg(UpdatePanel1, "你没有相应的操作权限，该操作要求必须权限为学院管理员或项目管理员。");
                return;
            }
            theresult = project.projectoperate(oldstatus, newstatus, int.Parse(pid));
            if (theresult == 1)
            {
                gridbind();
            }
            else if (theresult == 99)
            {
                PM.Code.Web.alertmessage.ShowMsg(UpdatePanel1, "数据库操作出错，请联系管理员。");
            }
            else if (theresult == 0)
            {
                PM.Code.Web.alertmessage.ShowMsg(UpdatePanel1, "操作失败，请检查权限是否正确。");
            }
        }
    }
}
